package io;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Cheng Jun
 * @version 1.0 Description: 学习地址：https://www.cnblogs.com/czwbig/p/10007289.html
 * @date 2019/10/23 15:37
 */
public class InputStreamExample {
    public static void main(String[] args) throws IOException {
        // 学习字节流FileInputStream的 read()
        // read();

        // byte[] bytes = new byte[4];
        // // 学习InputStream的 read(byte[] b)
        // read(bytes);
        // System.out.println();
        //
        // int off = 0;
        // int len = 4;
        // byte[] bytes1 = new byte[4];
        // // 学习InputStream的 read(byte b[], int off, int len)
        // read(bytes1, off, len);
        // System.out.println();

        // char为0的字符(转成String类型)是一个空字符""
        // 字符(Java Char)对应的ASCII码大全(十进制0 ~
        // 255)——https://blog.csdn.net/u012415035/article/details/91378717
        // byte[] bytes2 = {0, 53, 0, 53};
        // String bytes2String = new String(bytes2);
        // System.out.println(bytes2String);
        // char blank = 0;
        // System.out.println("blank" + String.valueOf(blank) + ".");

        // 学习InputStream的 mark(int readlimit) 和 reset()
        markAndReset();
    }

    /**
     * @throws IOException
     * @see ByteArrayInputStream#mark(int)
     */
    private static void markAndReset() throws IOException {
        // 创建 字节数组输入流
        InputStream byteArrayInputStream = new ByteArrayInputStream("123456789".getBytes());
        // 从输入流中读取单个字节，返回所读取的字节数据
        int byteData = byteArrayInputStream.read();
        int count = 0;
        while (byteData != -1) {
            System.out.print(((char) byteData));
            if (++count == 1) {
                // Note: The <code>readAheadLimit</code> for this class has no meaning.
                byteArrayInputStream.mark(2);
            }
            byteData = byteArrayInputStream.read();
        }

        // 测试reset()
        System.out.println();
        byteArrayInputStream.reset();
        int byteData1 = byteArrayInputStream.read();
        while (byteData1 != -1) {
            System.out.print(((char) byteData1));
            byteData1 = byteArrayInputStream.read();
        }
    }

    /**
     * 从输入流中最多读取len个字节的数据，并将其存储在字节数组中，并不是从数组起点开始，而是从off位置开始，返回实际读取的字节数
     *
     * @param bytes
     * @param off
     * @param len
     * @return
     * @throws IOException
     */
    private static int read(byte[] bytes, int off, int len) throws IOException {
        InputStream fileInputStream = new FileInputStream(".\\src\\io\\TestFile\\ForByte.txt");
        // the total number of bytes read into the buffer, or -1
        int byteNumber = fileInputStream.read(bytes, off, len);
        while (byteNumber != -1) {
            // 输出：This file is used for byte stream testing.in，这里最后一次读取时 g.，此时的bytes是stin，所以g.替换了st。
            // System.out.print(new String(bytes));
            // 每次读取，写到byte数组的元素
            System.out.print(new String(bytes, 0, byteNumber));
            byteNumber = fileInputStream.read(bytes, off, len);
        }
        return byteNumber;
    }

    /**
     * 从输入流中最多读取b.length个字节的数据，并将其存储在字节数组中，返回实际读取的字节数
     *
     * @param bytes
     * @return
     * @throws IOException
     */
    private static int read(byte[] bytes) throws IOException {
        InputStream byteArrayInputStream = new ByteArrayInputStream("123456789".getBytes());
        int byteNumber = byteArrayInputStream.read(bytes);
        while (byteNumber != -1) {
            // 输出：123456789678，这里最后一次读取时只有一个9，此时的bytes是5678，所以只有5被9给替换了。
            // System.out.print(new String(bytes));
            System.out.print(new String(bytes, 0, byteNumber));
            byteNumber = byteArrayInputStream.read(bytes);
        }
        return byteNumber;
    }

    /**
     * 从输入流中读取单个字节，返回所读取的字节数据。
     *
     * @return
     * @throws IOException
     */
    private static int read() throws IOException {
        // 字节流:使用相对路径打开文件
        FileInputStream fileInputStream = new FileInputStream(".\\src\\io\\TestFile\\ForByte.txt");
        // 使用绝对路径打开文件
        // InputStream fileInputStream = new
        // FileInputStream("D:\\gitee\\java\\src\\io\\TestFile\\ForByte.txt");
        int fileData = fileInputStream.read();
        while (fileData != -1) {
            System.out.print((char) fileData);
            // System.out.println(fileData);
            fileData = fileInputStream.read();
        }
        fileInputStream.close();
        return fileData;
    }
}
